import { createRouter, createWebHistory, RouteRecordRaw } from 'vue-router'
const routes: Array<RouteRecordRaw> =
  [
    {
      // 规则设置
      path: '/',
      name: 'rule',
      meta: {
        title: '规则设置',
        name: '规则设置'
      },
      component: () => import('@/views/Rule/Rule.vue')
    },
    // 积分获取
    {
      // 活动积分
      path: '/activityintegral',
      name: 'activityintegral',
      meta: {
        title: '积分获取',
        name: '活动积分'
      },
      component: () => import('@/views/GetIntegrals/ActivityIntegral.vue')
    },

    {
      // 促销积分
      path: '/promotionintegral',
      name: 'promotionintegral',
      meta: {
        title: '积分获取',
        name: '促销积分'
      },
      component: () => import('@/views/GetIntegrals/PromotionIntegral.vue')
    },

    // 积分消费
    {
      // 品类管理
      path: '/category',
      name: 'category',
      meta: {
        title: '积分消费',
        name: '品类管理'
      },
      component: () => import('@/views/XfIntegrals/Category.vue')
    },
    {
      // 商品管理
      path: '/commodity',
      name: 'commodity',
      meta: {
        title: '积分消费',
        name: '商品管理'
      },
      component: () => import('@/views/XfIntegrals/Commodity.vue')
    },
    {
      // 商城管理
      path: '/shoppingmall',
      name: 'shoppingmall',
      meta: {
        title: '积分消费',
        name: '商城管理'
      },
      component: () => import('@/views/XfIntegrals/ShoppingMall.vue')
    },
    {
      // 订单管理
      path: '/ordermanagement',
      name: 'ordermanagement',
      meta: {
        title: '积分消费',
        name: '订单管理'
      },
      component: () => import('@/views/XfIntegrals/Ordermanagement.vue')
    },

    // 积分数据
    {
      // 积分获取
      path: '/integralgain',
      name: 'integralgain',
      meta: {
        title: '积分数据',
        name: '积分获取'
      },
      component: () => import('@/views/DateIntegrals/IntegralGain.vue')
    },
    {
      // 积分变更
      path: '/integralalteration',
      name: 'integralalteration',
      meta: {
        title: '积分数据',
        name: '积分变更'
      },
      component: () => import('@/views/DateIntegrals/IntegralAlteration.vue')
    },
    {
      // 积分平衡
      path: '/integralbalance',
      name: 'integralbalance',
      meta: {
        title: '积分数据',
        name: '积分平衡'
      },
      component: () => import('@/views/DateIntegrals/IntegralBalance.vue')
    },
    {
      // 会员积分明细
      path: '/memberparticulars',
      name: 'memberparticulars',
      meta: {
        title: '积分数据',
        name: '会员积分明细'
      },
      component: () => import('@/views/DateIntegrals/MemberParticulars.vue')
    },
    {
      // 登录页
      path: '/login',
      name: 'login',
      meta: {
        name: '登录页'
      },
      component: () => import('@/views/Login/Login.vue')
    }
  ]

const router = createRouter({
  history: createWebHistory(),
  routes
})

/**
 * 全局前置路由守卫，每一次路由跳转前都进入这个 beforeEach 函数
 */
router.beforeEach((to, from, next) => {
  if (to.path == '/login') {
    // 登录或者注册才可以往下进行
    next();
  } else {
    // 获取 token
    const token = sessionStorage.getItem('token');
    // token 不存在
    if (token === null || token === '') {
      // ElMessage.error('您还没有登录，请先登录');
      next('/login');
    } else {
      next();
    }
  }
});

export default router
